﻿using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
namespace RdpClient
{
	public class SerializationHelper
	{
		public static string XmlSerialize(object o)
		{
			XmlSerializer xmlSerializer = new XmlSerializer(o.GetType());
			MemoryStream memoryStream = new MemoryStream();
			XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.Default);
			XmlSerializerNamespaces xmlSerializerNamespaces = new XmlSerializerNamespaces();
			xmlSerializerNamespaces.Add("", "");
			xmlSerializer.Serialize(xmlTextWriter, o, xmlSerializerNamespaces);
			xmlTextWriter.Close();
			return Encoding.Default.GetString(memoryStream.ToArray());
		}
		public static T XmlDeserialize<T>(string s)
		{
			XmlDocument xmlDocument = new XmlDocument();
			T result;
			try
			{
				xmlDocument.LoadXml(s);
				XmlNodeReader xmlReader = new XmlNodeReader(xmlDocument.DocumentElement);
				XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
				object obj = xmlSerializer.Deserialize(xmlReader);
				result = (T)((object)obj);
			}
			catch
			{
				result = default(T);
			}
			return result;
		}
		public static string Serialize(object o)
		{
			return SerializationHelper.XmlSerialize(o);
		}
		public static void Serialize<T>(string XMLFileToCreate, T instance)
		{
			if (instance != null)
			{
				XmlSerializerNamespaces xmlSerializerNamespaces = new XmlSerializerNamespaces();
				xmlSerializerNamespaces.Add("", "");
				XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
				using (StreamWriter streamWriter = new StreamWriter(XMLFileToCreate, false, Encoding.Default))
				{
					xmlSerializer.Serialize(streamWriter, instance, xmlSerializerNamespaces);
				}
			}
		}
		public static string Serialize<T>(T instance)
		{
			string result = "";
			MemoryStream memoryStream = null;
			StreamReader streamReader = null;
			try
			{
				XmlSerializer xmlSerializer = new XmlSerializer(instance.GetType());
				XmlWriterSettings xmlWriterSettings = new XmlWriterSettings();
				xmlWriterSettings.Encoding = Encoding.UTF8;
				xmlWriterSettings.OmitXmlDeclaration = true;
				memoryStream = new MemoryStream();
				XmlWriter xmlWriter2;
				XmlWriter xmlWriter = xmlWriter2 = XmlWriter.Create(memoryStream, xmlWriterSettings);
				try
				{
					XmlSerializerNamespaces xmlSerializerNamespaces = new XmlSerializerNamespaces();
					xmlSerializerNamespaces.Add("", "");
					xmlSerializer.Serialize(xmlWriter, instance, xmlSerializerNamespaces);
					memoryStream.Seek(0L, SeekOrigin.Begin);
					streamReader = new StreamReader(memoryStream);
					result = streamReader.ReadToEnd();
					xmlWriter.Close();
				}
				finally
				{
					if (xmlWriter2 != null)
					{
						((IDisposable)xmlWriter2).Dispose();
					}
				}
			}
			catch (Exception var_7_B6)
			{
				result = "";
			}
			finally
			{
				if (memoryStream != null)
				{
					memoryStream.Close();
					memoryStream.Dispose();
				}
				if (streamReader != null)
				{
					streamReader.Dispose();
				}
			}
			return result;
		}
		public static T DeSerialize<T>(string xmlFilePath)
		{
			XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
			T result;
			using (FileStream fileStream = new FileStream(xmlFilePath, FileMode.Open, FileAccess.Read, FileShare.Read))
			{
				result = (T)((object)xmlSerializer.Deserialize(fileStream));
			}
			return result;
		}
		public static T DeSerializeFromString<T>(string xmlString)
		{
			T result = default(T);
			try
			{
				XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
				using (MemoryStream memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(xmlString)))
				{
					result = (T)((object)xmlSerializer.Deserialize(memoryStream));
				}
			}
			catch (Exception var_3_52)
			{
				result = default(T);
			}
			return result;
		}
		public static byte[] SerializeObject(object obj)
		{
			byte[] result;
			if (obj == null)
			{
				result = null;
			}
			else
			{
				MemoryStream memoryStream = new MemoryStream();
				BinaryFormatter binaryFormatter = new BinaryFormatter();
				binaryFormatter.Serialize(memoryStream, obj);
				memoryStream.Position = 0L;
				byte[] array = new byte[memoryStream.Length];
				memoryStream.Read(array, 0, array.Length);
				memoryStream.Close();
				result = array;
			}
			return result;
		}
		public static object DeserializeObject(byte[] bytes)
		{
			object obj = null;
			object result;
			if (bytes == null)
			{
				result = obj;
			}
			else
			{
				MemoryStream memoryStream = new MemoryStream(bytes);
				memoryStream.Position = 0L;
				BinaryFormatter binaryFormatter = new BinaryFormatter();
				obj = binaryFormatter.Deserialize(memoryStream);
				memoryStream.Close();
				result = obj;
			}
			return result;
		}
	}
}
